package com.company;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;

public class SocketUtils {

    private int port = 3333;
    private static ServerSocket serverSocket;
    private static CallBack back;
    FixSizeLinkedList<String> list = null;

    private SocketUtils() {
    }

    private void init() {
        list = new FixSizeLinkedList<>(20);
        Properties pro = new Properties();
        InputStream in = null;
        try {
            in = this.getClass().getResourceAsStream("/config.properties");
            pro.load(new InputStreamReader(in, "utf-8"));
            port = Integer.parseInt(pro.getProperty("port", "20001"));
            System.out.println(port);
            startSocket();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void start(CallBack callBack) {
        if (serverSocket != null) {
            try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        back = callBack;
        new SocketUtils().init();
    }

    private void startSocket() {
        try {
            serverSocket = new ServerSocket(port);
            while (true) {
                Socket accept = serverSocket.accept();
                new Thread() {
                    @Override
                    public void run() {
                        InputStream inputStream = null;
                        try {
                            while (true) {
                                inputStream = accept.getInputStream();
                                byte[] bytes = new byte[1024];
                                inputStream.read(bytes);
                                String string = new String(bytes);
                                String code = string.trim();
                                JSONObject jsonObject = JSON.parseObject(string.trim());
                                if(jsonObject.containsKey("Code")){
                                    code=jsonObject.getString("Code");
                                }
                                if (TextUtils.isEmpty(code)) continue;
                                if (back != null && !list.contains(code)) {
                                    back.back(code);
                                    System.out.println("处理单号:" + code);
                                    list.add(code);
                                } else System.out.println("已处理单号:" + code);
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                if (inputStream != null)
                                    inputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void close() {
        try {
            if (serverSocket != null)
                serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}